package com.sxfq.loansupermarket.backsystem.modular.tcCenter.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.SearchCustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.PayServiceInfo;
import com.sxfq.loansupermarket.backsystem.modular.system.transfer.UserDto;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.dao.ConsultantDao;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.service.IConsultantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ConsultantServiceImpl implements IConsultantService {

    @Autowired
    private ConsultantDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<ConsultantInfo> selectPageListByQueryGetMap(Page<ConsultantInfo> page, SearchConsultantInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        if (StringUtil.isNotEmpty(searchInfo.getStartAppointmentTime())) {
            params.put("startAppointmentTime", searchInfo.getStartAppointmentTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndAppointmentTime())) {
            params.put("endAppointmentTime", searchInfo.getEndAppointmentTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartArrivalTime())) {
            params.put("startArrivalTime", searchInfo.getStartArrivalTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndArrivalTime())) {
            params.put("endArrivalTime", searchInfo.getEndArrivalTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReservations())) {
            params.put("reservations", searchInfo.getReservations());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndRevisitTime())) {
            params.put("endRevisitTime", searchInfo.getEndRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getRevisit())) {
            params.put("revisit", searchInfo.getRevisit());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        if (!"请选择".equals(searchInfo.getOneChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getOneChannel())) {
                params.put("oneChannel", searchInfo.getOneChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getTwoChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getTwoChannel())) {
                params.put("twoChannel", searchInfo.getTwoChannel());
            }
        }
        if (!"请选择".equals(searchInfo.getThrChannel())) {
            if (StringUtil.isNotEmpty(searchInfo.getThrChannel())) {
                params.put("thrChannel", searchInfo.getThrChannel());
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            params.put("customerStatus", searchInfo.getCustomerStatus());

        }
        String sql = "select roleid from user where id='" + ShiroKit.getUser().getId() + "'";
        UserDto userDto = sqlMapper.selectOne(sql, UserDto.class);
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if ("14".equals(userDto.getRoleid())) {
            params.put("fieldTc", "");
            params.put("appointmentPayService", "洁牙");
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsReturnVisit())) {
            params.put("isReturnVisit", searchInfo.getIsReturnVisit());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            if ("已成交".equals(searchInfo.getDealStatus())) {
                params.put("dealStatus", "t.deal_status ='已成交'");
            } else {
                params.put("dealStatus", "t.deal_status !='已成交'");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        params.put("tableName", this.getTableName());
        params.put("tableName1", tableName1);
        if (StringUtil.isNotEmpty(searchInfo.getBackVisit())) {
            return dao.getQueryQBackVisitPage(page, params);
        } else {
            return dao.getQueryPage(page, params);
        }
    }

    public ConsultantInfo selectOneById(String consultantId) {
        String sql = "select * from " + this.getTableName() + " where id= '" + consultantId + "'";
        ConsultantInfo info = sqlMapper.selectOne(sql, ConsultantInfo.class);
        return info;
    }


    public int update(ConsultantInfo info) {
        return dao.updateById(info);
    }

    public int insert(ConsultantInfo info) {
        String sql1 = "select * from " + this.getTableName() + " where id= '" + info.getId() + "'";
        ConsultantInfo info1 = sqlMapper.selectOne(sql1, ConsultantInfo.class);
        String sql = "insert into  `tc_diagnosis_info` (`customer_id`,`reservations_id`,`tc_name`,`diagnosis_content`,`diagnosis_time`) VALUES" +
                "('" + info.getId().trim() + "','" + info1.getReservationsId() + "','" + ShiroKit.getUser().getAccount() + "','" + info.getDiagnosisContent().trim() + "','" + DateTimeKit.now() + "');";
        return sqlMapper.insert(sql);
    }

    public int insert(CustomerInfo info) {
        String id = CommUtils.getUUID();
        String sql1 = "select * from " + this.getTableName() + " where id= '" + info.getId() + "'";
        ConsultantInfo info1 = sqlMapper.selectOne(sql1, ConsultantInfo.class);
        String sql = "insert into  `tc_diagnosis_info` (`customer_id`,`reservations_id`,`tc_name`,`diagnosis_content`,`diagnosis_time`) VALUES" +
                "('" + info.getId().trim() + "','" + info.getReservationsId() + "','" + ShiroKit.getUser().getAccount() + "','" + info.getDiagnosisContent().trim() + "','" + DateTimeKit.now() + "');";
        return sqlMapper.insert(sql);
    }

    public int insertNoDeal(DealInfo info) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "deal_info";
        String id = CommUtils.getUUID();
        String sql = "insert into  `" + tableName + "` (`id`,`reservations_id`,`customer_id`,`outpatient_name`,`tc_name`,`deal_status`,`no_deal_cause`,`remarks`,`operation_time`,`pay_service_id` ) VALUES" +
                "('" + id + "','" + info.getReservationsId() + "','" + info.getCustomerId() + "','" + info.getOutpatientName() + "','" + ShiroKit.getUser().getAccount() + "','未成交'" +
                ",'" + info.getNoDealCause() + "','" + info.getRemarks() + "','" + DateTimeKit.now() + "','" + info.getPayServiceId() + "' );";
        String deletesql = "delete from open_bill_info where tc_name='" + ShiroKit.getUser().getAccount() + "' and customer_id='" +
                info.getCustomerId() + "'";
        String sql1 = "insert into  `tc_diagnosis_info` (`customer_id`,`reservations_id`,`tc_name`,`diagnosis_content`,`diagnosis_time`) VALUES" +
                "('" + info.getCustomerId() + "','" + info.getReservationsId() + "','" + ShiroKit.getUser().getAccount() + "','" + info.getNoDealCause().trim() + "','" + DateTimeKit.now() + "');";
        sqlMapper.insert(sql1);
        sqlMapper.delete(deletesql);
        return sqlMapper.insert(sql);
    }

    public int updateDeal(DealInfo info) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        String sql = "update " + tableName1 + " a set a.deal_status='未成交' where a.reservations_id='" + info.getReservationsId() + "' and a.operation_time='" + info.getOperationTime() + "'" +
                "and a.outpatient_name='" + info.getOutpatientName() + "' ";
        return sqlMapper.update(sql);
    }

    public int insertDeal(PaymentInfo info) {
        //获取当前用户的前缀缩写，一个部门登录的客户对于的前缀不也一样，比方说武汉徐 东：wh_xd_
        String tableHead = ShiroKit.getUser().getAbbreviation();
        //String tableName = tableHead + "recharge_info";
        //徐东用户的正式开单记录表是：前缀+deal_info
        String tableName1 = tableHead + "deal_info";
        //String tableName2 = tableHead + "customer_info";
        String id = CommUtils.getUUID();
        String time = DateTimeKit.now();
        String a1=time.replace("-","");
        String a2=a1.replace(" ","");
        String a3=a2.replace(":","");
        String a4=tableHead.replace("_","");
        //点击开单操作后，把临时开单记录表的数据转移到正式开单记录表里面
        String insertSql = "insert into " + tableName1 + " " +
                "(id,customer_id,reservations_id,pay_service_id,tc_name,pay_service_num,referraler,service_actual_pay,remarks,deal_status,operation_time,get_time,outpatient_name,folding_pay,pay_service,left_up,left_down,right_up,right_down,doctor,nurse,type_number,id2) " +
                "select CONCAT('"+a4+"',id,'" + a3 + "'),customer_id,reservations_id,pay_service_id,tc_name,pay_service_num,referraler,service_actual_pay,remarks,'已开单','" + time + "','" + time + "','" + ShiroKit.getSessionAttr("outpatientName") +
                "',folding_pay,pay_service,left_up,left_down,right_up,right_down,doctor,nurse,type_number,CONCAT('"+a4+"',id,'" + a3 + "') from open_bill_info where customer_id='" + info.getCustomerId() + "' and tc_name='" + ShiroKit.getUser().getAccount() + "' ";
        sqlMapper.insert(insertSql);
        //删除当前操作客户中的所有历史开单记录
        String deletesql = "delete from open_bill_info where tc_name='" + ShiroKit.getUser().getAccount() + "' and customer_id='" +
                info.getCustomerId() + "'";
        sqlMapper.delete(deletesql);

        String itemsParam = info.getItemsParam();//获取各项的项目ID及实付金额拼接字符串
        String[] items = itemsParam.split("\\|");//首先按每项拆分 | 因为竖线是关键字，必须通过双斜线转义
        for (String curItem : items) {
            String[] curInfos = curItem.split(",");//获取每行的ID和实付金额
            String curServiceId = curInfos[0];
            String totalMoney = curInfos[1];
            String totalZheMoney = curInfos[2];
            String ids = curInfos[3];
            ids=a4+ids+a3;
            String updateSql = "update " + tableName1 + " set service_actual_pay= " + totalMoney + "" +
                    " ,folding_pay=" + totalZheMoney + ",arrears=folding_pay-service_actual_pay,remarks='" +
                    info.getArrearsRemarks() + "',remarks='" + info.getRemarkss() + "' where id='"+ids+"'";
            sqlMapper.update(updateSql);//根据预约ID、操作时间、项目ID来修改对应项目的实付金额和欠费金额
        }


        //以下整改逻辑是针对如果实付金额和折后金额不一致以后，那对于的开单记录里面的 每个项目实付金额都要按比率减少，比方说开单的总实付金额是10000，实际的折后金额是15000，那证明实际只付了部分10000/15000=0.66
        //以前是一种方案的计算，现在是多种，要进行按方案分租
//        String selectSql = "select sum(folding_pay) as foldingPay,type_number from "
//                + tableName1 + " where reservations_id='" + info.getReservationsId() + "'and operation_time='" + time + "' group by type_number";
//        List<PaymentInfo> paymentInfoList = sqlMapper.selectList(selectSql,
//                PaymentInfo.class);
//        for (PaymentInfo paymentInfo : paymentInfoList) {
//            Double sp = 0.00;
//            Double ap = 0.00;
//            if (paymentInfo.getFoldingPay() == 0) {
//                sp = 0.00;
//                ap = 0.00;
//            } else {
//                sp = info.getServiceActualPay() / paymentInfo.getFoldingPay();
//                ap = info.getArrears() / paymentInfo.getFoldingPay();
//            }
//            //计算出实付金额对应折后金额的比率后，相应正式开单记录表里面的实付金额都 按比率重新设置
//            String updateSql = "update " + tableName1 + " set service_actual_pay=folding_pay * " + sp + "" +
//                    " ,arrears=folding_pay*" + ap + " ,remarks='" +
//                    info.getArrearsRemarks() + "' where reservations_id='" + info.getReservationsId() +
//                    "' and operation_time='" + time + "' and type_number='" + paymentInfo.getTypeNumber
//                    () + "'";
//            sqlMapper.update(updateSql);
//
//            if(paymentInfo.getFoldingPay() != 0){//如果存在实付金额和原金额不一致，导致乘以百分百后，总的实付金额和欠款金额和计算后的实付金额、欠款金额有误差
//                //查询当前单据的所有项目开单记录，把最后一个项目的实付金额和欠款金额重新复制，保证和总的实付金额和欠款一直
//                String selectAll = "select * from "
//                        + tableName1 + " where reservations_id='" + info.getReservationsId() +
//                        "' and operation_time='" + time + "' and type_number='" + paymentInfo.getTypeNumber
//                        () + "'";
//                List<Map<String,Object>> dealList = sqlMapper.selectList(selectAll);
//                if(dealList.size() > 0){
//                    double serviceActualPayTotal = 0.00;//实付金额总和
//                    double arrearsTotal = 0.00;
//                    for(int i = 0;i < dealList.size();i++){
//                        Map<String,Object> currentDeal = dealList.get(i);//获取最后一行记录信息
//
//                        if(i == dealList.size() - 1){//最后一行
//                            double lastServiceActualPay = info.getServiceActualPay()  - serviceActualPayTotal;//计算最后一行的实付金额
//                            double lastArrears = info.getArrears() - arrearsTotal;//计算最后一行的欠费金额
//                            String updateLastSql = "update " + tableName1 + " set service_actual_pay=" + lastServiceActualPay + "" +
//                                    " ,arrears="+ lastArrears + "  where id='" + currentDeal.get("id") +"'";
//                            sqlMapper.update(updateLastSql);//通过ID来修改最后一行的实付金额和欠费金额
//                        }
//                        serviceActualPayTotal = serviceActualPayTotal + Double.parseDouble(currentDeal.get("service_actual_pay").toString());
//                        arrearsTotal = arrearsTotal + Double.parseDouble(currentDeal.get("arrears").toString());
//                    }
//                }
//            }
//
//        }
        return 1;
    }

    public int insertReturnVisit(CustomerInfo info) {
        String id = CommUtils.getUUID();
        String sql = "insert into  `return_visit_info` (`reservations_id`,`revisit`,`outpatient_name`, `revisit_content`,`revisit_time`) VALUES" +
                "('" + info.getReservationsId() + "','" + ShiroKit.getUser().getAccount() + "','" + ShiroKit.getUser().getOutpatientName() + "','" + info.getRevisitContent() + "'" +
                ",'" + DateTimeKit.now() + "');";
        return sqlMapper.insert(sql);
    }

    public int updateIsReturnVisit(CustomerInfo info) {
        String sql = "update " + this.getTableName() + " set is_return_visit=3,appointment_time=null where reservations_id='" + info.getReservationsId() + "'";
        return sqlMapper.update(sql);
    }

    public int updateDiagnosisStatus(String id) {
        String sql = "update " + this.getTableName() + " set diagnosis_status='已看诊' where id='" + id + "'";
        return sqlMapper.update(sql);
    }

    public List<ConsultantInfo> selectDiagnosisById(String customerId) {
        String sql = "select * from " + this.getTableName() + " where customer_id='" + customerId + "'";
        List<ConsultantInfo> list = sqlMapper.selectList(sql, ConsultantInfo.class);
        return list;
    }

    public List<PayServiceInfo> getPayServiceInfoList() {
        String sql = "select id,department from pay_service_info group by department order by id";
        List<PayServiceInfo> list = sqlMapper.selectList(sql, PayServiceInfo.class);
        return list;
    }

    public List<ConsultantInfo> getConsultantInfoList(String reservationsId) {
        String sql = "select id as id, customer_id as customerId,left(diagnosis_time,19) as diagnosisTime " +
                "from tc_diagnosis_info where reservations_id='" + reservationsId + "' order by diagnosis_time desc";
        List<ConsultantInfo> list = sqlMapper.selectList(sql, ConsultantInfo.class);
        return list;
    }

    public List<AddVisitedInfo> getRevisitRecordList(String reservationsId) {
        String sql = "select id as id, reservations_id as reservationsId,revisit_time as revisitTime " +
                "from return_visit_info where reservations_id='" + reservationsId + "' order by revisit_time desc";
        List<AddVisitedInfo> list = sqlMapper.selectList(sql, AddVisitedInfo.class);
        return list;
    }

    public List<DealInfo> getDealInfoInfoList(String reservationsId) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "deal_info";
        String sql = "select id as id, reservations_id as reservationsId,if(is_deal=1,'是','否') as isDealString,tc_name as tcName ," +
                " left(operation_time,19) as operationTime , is_deal as isDeal " +
                " from " + tableName1 + " where reservations_id='" + reservationsId + "' order by operation_time desc ";
        List<DealInfo> list = sqlMapper.selectList(sql, DealInfo.class);
        return list;
    }

    @Override
    public List<CustomerInfo> consultantExport(SearchCustomerInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            params.put("dealStatus", searchInfo.getDealStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartArrivalTime())) {
            params.put("startArrivalTime", searchInfo.getStartArrivalTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndArrivalTime())) {
            params.put("endArrivalTime", searchInfo.getEndArrivalTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReservations())) {
            params.put("reservations", searchInfo.getReservations());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartRevisitTime())) {
            params.put("startRevisitTime", searchInfo.getStartRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndRevisitTime())) {
            params.put("endRevisitTime", searchInfo.getEndRevisitTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getIsReturnVisit())) {
            params.put("isReturnVisit", searchInfo.getIsReturnVisit());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDoctor())) {
            params.put("doctor", searchInfo.getDoctor());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerStatus())) {
            params.put("customerStatus", searchInfo.getCustomerStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getDealStatus())) {
            if ("已成交".equals(searchInfo.getDealStatus())) {
                params.put("dealStatus", "t2.deal_status ='已成交'");
            } else {
                params.put("dealStatus", "t2.deal_status !='已成交'");
            }
        }
        if (StringUtil.isNotEmpty(searchInfo.getBackVisit())) {
            params.put("backVisit", "");
        } else {
            params.put("backVisit", "t2.customer_status!='复诊'");
        }
        params.put("tableName", this.getTableName());
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "recharge_info";
        String tableName2 = tableHead + "refund_info";
        params.put("tableName1", tableName1);
        params.put("tableName2", tableName2);
        return dao.getQueryPagesS(params);
    }

    @Override
    public List<CustomerInfo> consultantExportt(SearchCustomerInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartArrivalTime())) {
            params.put("startArrivalTime", searchInfo.getStartArrivalTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndArrivalTime())) {
            params.put("endArrivalTime", searchInfo.getEndArrivalTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFieldTc())) {
            params.put("fieldTc", searchInfo.getFieldTc());
        }
//        if (StringUtil.isNotEmpty(searchInfo.getBackVisit())) {
//            params.put("backVisit", "");
//        } else {
//            params.put("backVisit", "t2.customer_status!='复诊'");
//        }
        params.put("tableName", this.getTableName());
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "recharge_info";
        String tableName2 = tableHead + "refund_info";
        String tableName3 = tableHead + "deal_info";
        params.put("tableName1", tableName1);
        params.put("tableName2", tableName2);
        params.put("tableName3", tableName3);
        return dao.getQueryPages1(params);
    }

    @Override
    public List<CustomerInfo> consultantExporttt(SearchCustomerInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerName())) {
            params.put("customerName", searchInfo.getCustomerName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getCustomerPhone())) {
            params.put("customerPhone", searchInfo.getCustomerPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartAppointmentTime())) {
            params.put("startAppointmentTime", searchInfo.getStartAppointmentTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndAppointmentTime())) {
            params.put("endAppointmentTime", searchInfo.getEndAppointmentTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getFollowStatus())) {
            params.put("followStatus", searchInfo.getFollowStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getConsumptionStatus())) {
            params.put("consumptionStatus", searchInfo.getConsumptionStatus());
        }
        if (StringUtil.isNotEmpty(searchInfo.getRevisit())) {
            params.put("revisit", searchInfo.getRevisit());
        }
        if (StringUtil.isNotEmpty(searchInfo.getBackVisit())) {
            params.put("backVisit", "");
        } else {
            params.put("backVisit", "t2.customer_status!='复诊'");
        }
        params.put("tableName", this.getTableName());
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName1 = tableHead + "recharge_info";
        String tableName2 = tableHead + "refund_info";
        params.put("tableName1", tableName1);
        params.put("tableName2", tableName2);
        return dao.getQueryPages2(params);
    }

    public String getTableName() {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName = tableHead + "customer_info";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return tableName;
    }
}
